import rectanglemark from './rectanglemark'
import polygonmark from './polygonmark'
import { EventCode, getMouseInfo } from "./utiles"

class drawtools {
  constructor() {
    this._viewer = window.viewer
    // this._options = options
    // this._layer = new Cesium.CustomDataSource('plotlayer')
    // this._viewer.dataSources.add(this._layer)
    // this._handler = new Cesium.ScreenSpaceEventHandler(
    //   viewer.canvas,
    // )
    this._handler = new Cesium.ScreenSpaceEventHandler(
      viewer.scene.canvas,
    )
  }

  draw(options = {}) {
    let drawentity = null
    this._layer = options._layer
    this._options = options
    switch (options.type) {
      case 'polygonmark':
        drawentity = new polygonmark({viewer: window.viewer, ...options})
        break
      case 'rectanglemark':
        drawentity = new rectanglemark({viewer: window.viewer, ...options})
        break

      default:
        break
    }
    console.log(drawentity.entity);
    options.plotlist[drawentity.id] = drawentity
    this._layer.entities.add(drawentity.entity)
    this.drawstart(drawentity, options)
  }

  drawstart(e, o = {}) {
    // 新建实体

    let pos = []
    let that = this

    let viewer = window.viewer
    //获取 事件
    let LEFT_CLICK = viewer.screenSpaceEventHandler.getInputAction(
      Cesium.ScreenSpaceEventType.LEFT_CLICK,
    )
    let LEFT_DOUBLE_CLICK = viewer.screenSpaceEventHandler.getInputAction(
      Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK,
    )
    let MOUSE_MOVE = viewer.screenSpaceEventHandler.getInputAction(
      Cesium.ScreenSpaceEventType.MOUSE_MOVE,
    )

    let body = document.querySelector('body') // 这里是把整个页面的鼠标指针都变了，可以指定某个元素范围内
    body.style.cursor = `crosshair`
    
    function moveaction(movement) {
      
      if (pos.length > 0) {
        pos.pop()
        pos.push(getMouseInfo(movement.endPosition).surfacePosition)
        e.setPositions(pos)
      }
    }
    
    function leftaction(movement) {
      // getMouseInfo(movement.position)
      
      if (pos.length == 2 && that._options.type == 'rectanglemark') {
        that._handler.removeInputAction(EventCode['mouseMove'])
        that._handler.removeInputAction(EventCode['leftClick'])
        that._handler.removeInputAction(EventCode['rightClick'])
        that._handler.setInputAction(LEFT_CLICK, EventCode['leftClick'])
        that._handler.setInputAction(MOUSE_MOVE, EventCode['mouseMove'])
        body.style.cursor = `default`
        that.drawend(e, o)
      }else{
        if (pos.length == 0) {
          pos.push(getMouseInfo(movement.position).surfacePosition)
        }
        pos.push(getMouseInfo(movement.position).surfacePosition)
        e.setPositions(pos)
      }
      
    }
    
    function rightaction(movement) {
      pos.pop()
      e.setPositions(pos)
      that._handler.removeInputAction(EventCode['mouseMove'])
      that._handler.removeInputAction(EventCode['leftClick'])
      that._handler.removeInputAction(EventCode['rightClick'])
      that._handler.setInputAction(LEFT_CLICK, EventCode['leftClick'])
      body.style.cursor = `default`
      that.drawend(e, o)
    }
    that._handler.removeInputAction(EventCode['mouseMove'])
    this._handler.setInputAction(moveaction, EventCode['mouseMove'])
    body.style.cursor = `crosshair`
    
    this._handler.setInputAction(leftaction, EventCode['leftClick'])
    this._handler.setInputAction(rightaction, EventCode['rightClick'])
    // removeInputAction
  }
  
  drawend(e, o) {
    o.c(e)
  }
}

export default drawtools
